from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import os

from app.api.v1.endpoints import articles, upload, auth, tags

app = FastAPI(
    title="个人博客API",
    description="基于FastAPI的个人博客后端API",
    version="1.0.0"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 在生产环境中应该设置具体的域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 确保上传目录存在
os.makedirs("static/uploads", exist_ok=True)

# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")

# 注册路由
app.include_router(articles.router, prefix="/api/v1/articles", tags=["文章相关路由"])
app.include_router(upload.router, prefix="/api/v1/upload", tags=["文件上传"])
app.include_router(auth.router, prefix="/api/v1/auth", tags=["auth"])
app.include_router(tags.router, prefix="/api/v1/tags", tags=["tags"])

@app.get("/")
async def root():
    return {"message": "欢迎访问个人博客API"} 